<?php
/**
 * 文章控制器
 * User: Lin Hui
 * Date: 2017/5/17
 * Time: 17:20
 */

namespace Admin\Controller;


use Common\Model\WebsiteModel;

class ArticleController extends BaseController
{
    /**
     * 列表页排序
     * @return string
     */
    public function getSort()
    {
        return 'create_time desc';
    }

    /**
     *获取条件
     */
    public function _filter($model)
    {
        $fields = $model->getDbFields();
        $map = array();
        $request = I('request.');
        //逐个对应条件
        foreach($request as $key => $item) {
            if (in_array($key, $fields) && !is_array($item) && $item !== null && $item !== '') {
                $map[$key] = $item;
            }
        }
        //模糊查询
        if(is_array($request['like'])) {
            foreach($request['like'] as $key => $item) {
                if($item){
                    $map[$key] = array('like', "%{$item}%");
                }
            }
        }
        if($request['category_id']) {
            $attr_model = M('article_attributes');
            $article_id = $attr_model->where(array('type'=>array_search('category', C('ARTICLE.attribute')), 'value'=>$request['category_id']))->getField('article_id', true);
            if($article_id){
                $map['article_id'] = array('in', $article_id);
            } else {
                $map['article_id'] = 0;
            }

        }
        $this->assign('map', $request);
        return $map;
    }
    /**
     * 列表页回调
     * @param $list
     * @param $map
     */
    public function indexCallback(&$list, $map)
    {
        $attr_model = new \Common\Model\ArticleAttributesModel();
        $category_model = new \Common\Model\CategoryModel();
        $category = $category_model->getField('category_id, category_name');
        $type_cache = array();
        foreach($list as $k=> $item) {
            $attributes = $attr_model->getAttributes($item['article_id']);//文章属性
            $list[$k]['category'] = array();//分类
            foreach($attributes['category'] as $attribute) {
                if(!$type_cache['category'][$attribute]){
                    $type_cache['category'][$attribute] = $category_model->where(array('category_id'=>$attribute))->getField('category_name');
                }
                $list[$k]['category'][] = $type_cache['category'][$attribute];
            }

        }

        $this->assign('category', $category);

    }

    /**
     * 文章编辑页
     */
    public function edit()
    {
        $attr_model = new \Common\Model\ArticleAttributesModel();
        $model = M('article');
        if($article_id = I('article_id', 0, 'intval')) {
            $attr = $attr_model->getAttributes($article_id);//属性
            $info = $model->find($article_id);//文章信息
        }

        $category = (new \Common\Model\CategoryModel())->getCategorySimple(); //分类

        if (IS_AJAX) {
            $data = I('post.');
            $mode = 1;//模式 1 添加 2 修改
            if (is_numeric($data['article_id']) && $data['article_id'] > 0) {
                $mode = 2;
                $article_id = $data['article_id'];
            }
            $data['create_time'] = strtotime($data['create_time']) ? strtotime($data['create_time']) : time();
            $data['update_time'] = time();

            //视频ID识别
            if($data['video_url']){
                if(preg_match('/id_(.*?)\.html/', $data['video_url'], $matches)) {
                    $data['video_id'] = $matches[1];
                } else {
                    $this->responseAjax('识别视频链接失败');
                }
            }
            //无缩略图取内容中第一张图
            if(!$data['thumb']) {
                $content = htmlspecialchars_decode($data['content']);
                preg_match('/<img.*?src="(.*?)".*?>/', $content, $matches);
                $data['thumb'] = $matches[1];
            }

            //来源链接处理
            if($data['website']) {
                $data['website'] = prefixUrl($data['website']);
            }
            if (
            $model->create($data) //数据自动处理
            &&
            $mode == 1
                ? ($article_id = $model->add()) //添加
                : $model->where(array('article_id'=>$article_id))->save() !== false //修改
            ) {

                //保存分类
                if(!$attr_model->setAttributes($article_id, 'category', $data['category_id'])) {
                    $this->responseAjax('分类分配失败');
                }

                $this->responseAjax('保存成功', true, array(), U('edit', 'article_id='.$article_id));
            } else {
                $this->responseAjax('保存失败');
            }

        }

        $this->assign('user', $this->user);
        $this->assign('attr', $attr);
        $this->assign('info', $info);
        $this->assign('category', $category);
        $this->assign('website_info', getWebsiteInfo());
        $this->assign('qiniu', getQiniuInfo());
        $this->display();
    }

    /**
     *推荐
     */
    public function recommend()
    {
        if(IS_AJAX) {
            $article_id = I('article_id', 0, 'intval');
            $model = M('article');
            $info = $model->find($article_id);
            $status = I('status') == 1 ? 1 : 0;
            if($info){
                if($model->where(array('article_id'=>$article_id))->save(array('recommend'=>$status))){
                    $this->responseAjax('操作成功', true, array(), U('index'));
                } else {
                    $this->responseAjax('操作失败');
                }
            } else {
                $this->responseAjax('文章不存在');
            }
        }
    }
    /**
     *置顶
     */
    public function top()
    {
        if(IS_AJAX) {
            $article_id = I('article_id', 0, 'intval');
            $model = M('article');
            $info = $model->find($article_id);
            $status = I('status') == 1 ? 1 : 0;
            if($info){
                if($model->where(array('article_id'=>$article_id))->save(array('top'=>$status))){
                    $model->where(array('article_id'=>array('neq', $article_id)))->save(array('top'=>0));
                    $this->responseAjax('操作成功', true, array(), U('index'));
                } else {
                    $this->responseAjax('操作失败');
                }
            } else {
                $this->responseAjax('文章不存在');
            }
        }
    }
}